#pragma once
#include <iostream>
#include <unordered_map>
#include <functional>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

using task_t = std::function<void()>;

void func1()
{
    std::cout << "执行任务1" << std::endl;
}

void func2()
{
    std::cout << "执行任务2" << std::endl;
}
void func3()
{
    std::cout << "执行任务3" << std::endl;
}
void func4()
{
    std::cout << "执行任务4" << std::endl;
}

//任务
class Task
{
public:
    //初始化
    Task()
    {
        srand(time(nullptr));
        mp[0] = func1;
        mp[1] = func2;
        mp[2] = func3;
        mp[3] = func4;
    }
    
    //获取任务号
    int SelectTask()
    {
        return rand() % mp.size();
    }

    //添加任务
    void AddTask(task_t t)
    {
        mp[mp.size()] = t;
    }

    //执行任务
    void Excute(int number)
    {
        if(mp.find(number) == mp.end())
            return;
        
        mp[number]();
    }

private:
    //编号与任务的隐式哈希
    std::unordered_map<int, task_t> mp;
};

//全局
Task tk;


//实行的方法
void Worker()
{
    while (true)
    {
        int num;
        int n = read(0,&num,sizeof(num));
        if(n > 0)
        {
            tk.Excute(num);
        }
        else if(n == 0)
        {
            break;
        }
    }
}